package com.my.ads.lib.bean;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.my.ads.lib.activity.WeatherActivity;
import com.rapid.clean.deep.R;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by xuanyang on 2018/3/8.
 */

public class RecyclerAdapterWeather extends RecyclerView.Adapter<ViewHolderWeather> {
    private List<WeatherBean> itemList;
    private Context context;

    public RecyclerAdapterWeather(Context context, List<WeatherBean> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public ViewHolderWeather onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.weather_list_row, null);

        ViewHolderWeather viewHolder = new ViewHolderWeather(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolderWeather customViewHolder, int i) {

        WeatherBean weatherItem = itemList.get(i);

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        // Temperature
        float temperature = UnitConvertor.convertTemperature(Float.parseFloat(weatherItem.getTemperature()), sp);

        if (sp.getBoolean("temperatureInteger", false)) {


            temperature = Math.round(temperature);


        }

        // Rain
        double rain = Double.parseDouble(weatherItem.getRain());
        String rainString = UnitConvertor.getRainString(rain, sp);

        // Wind
        double wind;
        try {
            wind = Double.parseDouble(weatherItem.getWind());
        } catch (Exception e) {
            e.printStackTrace();
            wind = 0;
        }
        wind = UnitConvertor.convertWind(wind, sp);

        // Pressure
        double pressure = UnitConvertor.convertPressure((float) Double.parseDouble(weatherItem.getPressure()), sp);

        TimeZone tz = TimeZone.getDefault();
        String defaultDateFormat = context.getResources().getStringArray(R.array.dateFormatsValues)[0];
        String dateFormat = sp.getString("dateFormat", defaultDateFormat);
        if ("custom".equals(dateFormat)) {
            dateFormat = sp.getString("dateFormatCustom", defaultDateFormat);
        }
        String dateString;
        try {
            SimpleDateFormat resultFormat = new SimpleDateFormat(dateFormat);
            resultFormat.setTimeZone(tz);
            dateString = resultFormat.format(weatherItem.getDate());
        } catch (IllegalArgumentException e) {
            dateString = context.getResources().getString(R.string.error_dateFormat);
        }

        if (sp.getBoolean("differentiateDaysByTint", false)) {
            Date now = new Date();
            /* Unfortunately, the getColor() that takes a theme (the next commented line) is Android 6.0 only, so we have to do it manually
             * customViewHolder.itemView.setBackgroundColor(context.getResources().getColor(R.attr.colorTintedBackground, context.getTheme())); */
            int color;
            if (weatherItem.getNumDaysFrom(now) > 1) {
                TypedArray ta = context.obtainStyledAttributes(new int[]{R.attr.colorTintedBackground, R.attr.colorBackground});
                if (weatherItem.getNumDaysFrom(now) % 2 == 1) {
                    color = ta.getColor(0, context.getResources().getColor(R.color.colorTintedBackground));
                } else {
                    /* We must explicitly set things back, because RecyclerView seems to reuse views and
                     * without restoring back the "normal" color, just about everything gets tinted if we
                     * scroll a couple of times! */
//                    color = ta.getColor(1, context.getResources().getColor(R.color.colorBackground));
                }
                ta.recycle();
//                customViewHolder.itemView.setBackgroundColor(color);
            }
        }

        customViewHolder.itemDate.setText(dateString);
        if (sp.getBoolean("displayDecimalZeroes", false)) {

            customViewHolder.itemTemperature.setText(new DecimalFormat("#.0").format(temperature) + " " + "℉");
        } else {

            customViewHolder.itemTemperature.setText(new DecimalFormat("#.#").format(temperature) + " " + "℉");
        }
        customViewHolder.itemDescription.setText(weatherItem.getDescription().substring(0, 1).toUpperCase() +
                weatherItem.getDescription().substring(1) + rainString);
        Typeface weatherFont = Typeface.createFromAsset(context.getAssets(), "fonts/weather.ttf");
//        Log.d("Typeface","第"+i+"个天气");
//        Log.d("Typeface",weatherItem.getIcon().toString().toString());
        if (weatherItem.getIcon().equals(context.getString(R.string.weather_sunny))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_sun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_sun));
//            Log.d("Typeface","太阳");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_clear_night))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_night_tb);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_night));
//            Log.d("Typeface","月亮");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_foggy))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_sun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_qita));
//            Log.d("Typeface","雾");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_cloudy))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_yun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_qita));
//            Log.d("Typeface","多云的");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_rainy))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_sun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_qita));
//            Log.d("Typeface","多雨的");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_snowy))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_sun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_qita));
//            Log.d("Typeface","下雪的");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_thunder))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_sun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_qita));
//            Log.d("Typeface","打雷");
        }else if (weatherItem.getIcon().equals(context.getString(R.string.weather_drizzle))){
            customViewHolder.itemsIcon.setImageResource(R.drawable.ic_sun_tubiao);
            customViewHolder.weather_bg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_qita));
//            Log.d("Typeface","下蒙蒙细雨");
        }
        customViewHolder.itemIcon.setTypeface(weatherFont);
        customViewHolder.itemIcon.setText(weatherItem.getIcon());

        if (sp.getString("speedUnit", "m/s").equals("bft")) {
            customViewHolder.itemyWind.setText(context.getString(R.string.wind) + ": " +
                    UnitConvertor.getBeaufortName((int) wind) + " " + WeatherActivity.getWindDirectionString(sp, context, weatherItem));
        } else {
            customViewHolder.itemyWind.setText(context.getString(R.string.wind) + ": " + new DecimalFormat("#.0").format(wind) + " " +
                    WeatherActivity.localize(sp, context, "speedUnit", "m/s")
                    + " " + WeatherActivity.getWindDirectionString(sp, context, weatherItem));
        }
        customViewHolder.itemPressure.setText(context.getString(R.string.pressure) + ": " + new DecimalFormat("#.0").format(pressure) + " " +
                WeatherActivity.localize(sp, context, "pressureUnit", "hPa"));
        customViewHolder.itemHumidity.setText(context.getString(R.string.humidity) + ": " + weatherItem.getHumidity() + " %");
    }

    @Override
    public int getItemCount() {
        return (null != itemList ? itemList.size() : 0);
    }
}
